/**
 * Created by bethinner on 5/17/16.
 */
import React, { Component } from 'react';
import {bindActionCreators} from 'redux'
import { connect } from 'react-redux'
import Link from 'react-router/lib/Link'

import OptionBar from '../components/Option/OptionBar'
import NavigationBar from '../components/NavigationBar'
import ListView from '../components/ListView'
import * as actions from '../actions/actions.js'
import cookie from  'react-cookie'
import {browserHistory} from "react-router"

class MyCouponsContainer extends Component {
    constructor(props) {
        super(props);
        this._chose = this.chose.bind(this);
    }

    componentWillReceiveProps(nextProps){
        //console.log("nextProps",nextProps);
    }

    componentWillMount() {
        var type=1;
        if (null!=this.props.location.state){
            type=this.props.location.state.type;
        }else if (null!=this.props.location.query.type){
            type=Number(this.props.location.query.type);
        }
        //console.log("will mount",type);
        this.setState({type:Number(type)});
        window.sessionStorage.setItem("couponType",type);
        $('#loadingToast').hide();
    }

    componentWillUnmount() {
        //this.props.cleanMyNotPayListFetch();
        //this.props.cleanMyUnusedListFetch();
        //this.props.cleanMyUsedCardsFetch();
        //this.props.cleanMyOverDueCardsFetch();
    }

    chose(type) {
        window.sessionStorage.setItem("couponType",type);
        this.setState({type: type });
    }


    goBack(){
        if(window.sessionStorage.getItem("retUrl")==null){
            browserHistory.goBack();
        }else{
            browserHistory.push({
                pathname: window.sessionStorage.getItem("retUrl"),
            });
        }
    }
    render() {

        var mobile=cookie.load('mobile');
        let dataL = {};

        //console.log("this.unuse",this.props.myUnusedCardList);
        switch (this.state.type) {
            case 0:
                if (null == this.props.myNotPayList.get('code')||-1==this.props.myNotPayList.get('code')) {
                    this.props.getMyNotPayListFetch(mobile, -1);
                }
                dataL = this.props.myNotPayList;
                break;
            case 1:
                if (null == this.props.myUnusedCardList.get('code')||-1==this.props.myUnusedCardList.get('code')) {
                    this.props.getMyUnusedListFetch(mobile, 0);
                }
                dataL = this.props.myUnusedCardList;
                break;
            case 2:
                if (null == this.props.myUsedCardList.get('code')||-1== this.props.myUsedCardList.get('code')) {
                    this.props.getMyUsedCardsFetch(mobile, 1);
                }
                dataL = this.props.myUsedCardList;
                break;
            case 3:
                if (null == this.props.myOverdueCardList.get('code')||-1== this.props.myOverdueCardList.get('code')) {
                    this.props.getMyOverDueCardsFetch(mobile, 2);
                }
                dataL = this.props.myOverdueCardList;
                break;
        }
        return (
            <div>
                <NavigationBar Title="我的卡券" LeftBar="true" clickFunc={this.goBack}/>
                <div style={{position:"fixed",
                zIndex:"999999",width:"100%",height:"45px"}}>
                    <OptionBar chose={this._chose}
                               items={["待支付","可使用","已使用","已过期"]}
                               type={this.state.type}
                    />
                </div>

                    <div style={{margin:"59px 18px 0px 18px"}}>
                        <ListView  dataList={dataL} itemType={this.state.type}/>
                    </div>
            </div>

        )
    }
}

function mapStateToProps(state) {
    $('#loadingToast').hide();
    return {
        myNotPayList: state.get('myNotPayList'),
        myUnusedCardList: state.get('myUnusedCardList'),
        myUsedCardList: state.get('myUsedCardList'),
        myOverdueCardList: state.get('myOverdueCardList'),
    }


}
function mapDispatchToProps(dispatch) {

    return bindActionCreators(actions, dispatch);
}
MyCouponsContainer.defaultProps = {}

MyCouponsContainer.propTypes = {}
module.exports=connect(mapStateToProps, mapDispatchToProps)(MyCouponsContainer);

